package com.example.labtest.service.impl;
import com.example.labtest.dao.AdminRepository;
import com.example.labtest.dao.StudentRepository;
import com.example.labtest.domain.Admin;
import com.example.labtest.domain.Student;
import com.example.labtest.domain.Student_info;
import com.example.labtest.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class AdminImplService implements AdminService {

    @Autowired
    AdminRepository adminRepository;
    StudentRepository studentRepository;

    //实现接口

    public List<Admin> findByAdmnameAndAdmpsw(String admname, String admpsw){
        return adminRepository.findByAdmnameAndAdmpsw(admname,admpsw);
    }


    /**
     * 读取所有的管理员数据
     * @return
     */
    public List<Admin>findAll1()
    {
        return adminRepository.findAll();
    }


    /**
     * 读取所有的学生数据
     * @return
     */
    public List<Student>findAll()
    {
        return studentRepository.findAll();
    }

    /**
     * 只支持分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){

        return studentRepository.findAll(pageable);
    };

    /**
     * 分页，带过滤的查询,按照Student来过滤
     */
    public Page<Student>findAll(Example<Student> student, Pageable pageable)
    {
        return studentRepository.findAll(student,pageable);
    }


    //实现接口

    public List<Student>findByNameAndPsw(String sname,String psw){
        return studentRepository.findBySnameAndPsw(sname,psw);

    }

    /**
     * 插入学生
     * @param student
     * @return
     */
    /**
     *
     * @Transactional可以说是spring中最常用的注解之一了，
     * 通常情况下我们在需要对一个service方法添加事务时，加上这个注解，如果发生unchecked
     *exception，就会发生rollback，最典型的例子如下。
     */
    @Transactional
    public Student insert(Student student)
    {
        return studentRepository.save(student);
    }

    /**
     * 更新学生
     * @param student
     * @return
     */
    @Transactional
    public Student update(Student student)
    {
        return studentRepository.save(student);
    }

    /**
     * 删除学生
     * @param id
     */
    @Transactional
    public void delete1(Long id){studentRepository.deleteById(id);}


    /**
     * 删除学生
     */

    public void delete2(Student student)
    {
        studentRepository.delete(student);
    }

    public void delete(Long id){
        Student student=new Student();
        student.setId(id);
        studentRepository.delete(student);
    }

    /**
     * 查找管理员
     * @param id
     * @return
     */
    @Transactional
    public Admin search(Long id){return adminRepository.findById(id).get();}

    @Transactional
    public Student getById(Long id)
    {
        Student student=studentRepository.findById(id).orElse(null);
        return student;
    }

    /**
     * 按名字查询学生信息
     * @param name
     * @return
     */
    @Transactional
    public List<Student> findByName(String name){

        return studentRepository.findBySname(name);

    }

    public Page<Student>findByNameLike(String name,Pageable pageable){
        return studentRepository.findBySnameLike(name,pageable);
    }

}
